﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Money.Interfaces;
using Money.Data;
using One.Framework;
using One.Web;

public partial class Admin_ChooseMemorizedTransaction : System.Web.UI.Page
{
    #region fields

    private IMemorizedTransactionService memorizedTransactionService;
    protected List<MemorizedTransaction> listMemorizedTransactions;
    private MemorizedTransactionOrderBy orderBy;
    private OrderDirection orderDirection;
    private string redirectUrl = "ChooseMemorizedTransaction.aspx";
    private int totalRecords;

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        memorizedTransactionService = ServiceFactory.Create<IMemorizedTransactionService>();

        GetParams();
        LoadData();
        if (!IsPostBack)
            UpdateView();
    }

    protected void SelectMemorizedTransaction(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.Form["selectedItem"]))
            return;
        Response.Redirect(string.Format("{0}?memorized={1}", "EditTransaction.aspx", Request.Form["selectedItem"]));
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        orderBy = QueryStringUtility.GetEnum<MemorizedTransactionOrderBy>("orderBy", MemorizedTransactionOrderBy.Id.ToString());
        orderDirection = QueryStringUtility.GetEnum<OrderDirection>("orderDirection", OrderDirection.ASC.ToString());
    }

    private void LoadData()
    {
        string strOrderBy = orderBy.ToString();
        if (orderBy == MemorizedTransactionOrderBy.Account || orderBy == MemorizedTransactionOrderBy.Category || orderBy == MemorizedTransactionOrderBy.Ref || orderBy == MemorizedTransactionOrderBy.Class)
            strOrderBy = string.Format("{0}.{1}", strOrderBy, orderBy == MemorizedTransactionOrderBy.Account ? "DisplayName" : "Name");
        listMemorizedTransactions = memorizedTransactionService.GetMemorizedTransactions(WebSession.CurrentUser.UserId, strOrderBy, orderDirection.ToString(), pager.PageIndex,
                                MoneyManagerConfig.AdminPageSize, out totalRecords);
    }

    private void UpdateView()
    {
        repeaterTransactions.DataSource = listMemorizedTransactions;
        repeaterTransactions.DataBind();

        (Master as Admin_MasterPage).SelectedTab = "transactionManagement";
        pageTitle.Text = Resources.Strings.ChooseMemorizedTransaction;
        pager.TotalRecords = totalRecords;
        pager.PageSize = MoneyManagerConfig.AdminPageSize;
    }

    #endregion

    #region public functions

    public string GetOrderByLink(string propertyName)
    {
        OrderDirection newOrderDirection = orderDirection;
        string url = CreateUrlParams();
        if (propertyName == orderBy.ToString())
        {
            newOrderDirection = orderDirection == OrderDirection.ASC ? OrderDirection.DESC : OrderDirection.ASC;
        }
        return String.Format("{0}{4}OrderBy={1}&OrderDirection={2}&Page={3}", url,
            propertyName, newOrderDirection.ToString(), pager.PageIndex, url.IndexOf('?') > 0 ? "&" : "?");
    }

    private string CreateUrlParams()
    {
        return redirectUrl;
    }

    public string GetOrderDirectionIndicator(string propertyName)
    {
        if (propertyName == orderBy.ToString())
        {
            return String.Format("<img src=\"../images/{0}.gif\" alt=\"{0}\" align=\"absmiddle\" />", orderDirection);
        }
        return "";
    }

    public string GetNameTransactionType(object value)
    {
        try
        {
            return Enum.GetName(typeof(TransactionType), value);
        }
        catch
        {
            return string.Empty;
        }
    }

    #endregion
}
